import time
from selenium.webdriver.chrome import webdriver
from selenium.webdriver.common.by import By

from common.doLog import DoLog

logger = DoLog().set_get_logger()
class OrderPage():
    try:
        logger.error("订单结算功能开始运行")
        sphoto = (By.XPATH, '/html/body/div[4]/div/div[2]/div[1]/div[2]/ul[1]/li[1]/a/img')
        shop = (By.XPATH, '//*[@id="ECS_FORMBUY"]/ul[3]/li[2]/a')
        setment = (By.XPATH, '/html/body/div[5]/div[1]/table/tbody/tr/td[2]/a/img')
        sping = (By.XPATH, '//*[@id="loginForm"]/table/tbody/tr[5]/td/div/input[2]')
        spro1 = (By.XPATH, '//*[@id="selProvinces_0"]')
        spro2 = (By.XPATH, '//*[@id="selProvinces_0"]/option[17]')
        scit1 = (By.XPATH, '//*[@id="selCities_0"]')
        scit2 = (By.XPATH, '//*[@id="selCities_0"]/option[2]')
        sdtr1 = (By.XPATH, '//*[@id="selDistricts_0"]')
        sdtr2 = (By.XPATH, '//*[@id="selDistricts_0"]/option[5]')
        unameE = (By.NAME, "consignee")
        uemailE = (By.NAME, "email")
        uaddE = (By.NAME, "address")
        uphoneE = (By.NAME, "tel")
        regE = (By.NAME, "Submit")
        spTab = (By.XPATH, '//*[@id="shippingTable"]/tbody/tr[2]/td[1]/input')
        payTab = (By.XPATH, '//*[@id="paymentTable"]/tbody/tr[3]/td[1]/input')
        tFor = (By.XPATH, '//*[@id="theForm"]/div[9]/table/tbody/tr[3]/td[2]/label[1]/input')
        tFor2 = (By.XPATH, '//*[@id="theForm"]/div[11]/div[2]/input[1]')
        def dz(self, a, b, c, d):
            #a:用户名 b:邮箱 c:地址 d:手机号
            dd = webdriver.WebDriver()
            # 通过实例打开网页
            dd.get("http://192.168.106.130/index.php")
            # 点击进入商品详细界面
            dd.find_element(*self.sphoto).click()
            # 加入购物车
            dd.find_element(*self.shop).click()
            time.sleep(3)
            # 结算
            dd.find_element(*self.setment).click()
            # 选择不登录直接购买
            dd.find_element(*self.sping).click()
            # 输入订单地址信息
            dd.find_element(*self.spro1).click()
            dd.find_element(*self.spro2).click()
            dd.find_element(*self.scit1).click()
            dd.find_element(*self.scit2).click()
            dd.find_element(*self.sdtr1).click()
            dd.find_element(*self.sdtr2).click()
            dd.find_element(*self.unameE).send_keys(a)
            dd.find_element(*self.uemailE).send_keys(b)
            dd.find_element(*self.uaddE).send_keys(c)
            dd.find_element(*self.uphoneE).send_keys(d)
            dd.find_element(*self.regE).click()
            # 选择上门取货
            dd.find_element(*self.spTab).click()
            # 选择货到付款
            dd.find_element(*self.payTab).click()
            # 选择缺货时等待所有商品备齐后再发
            dd.find_element(*self.tFor).click()
            time.sleep(3)
            # 提交订单
            dd.find_element(*self.tFor2).click()
    except Exception as e:
        logger.error(f"订单结算功能运行异常，异常为{e}")
    else:
        logger.error("订单结算功能运行正常")
    finally:
        logger.error("订单结算功能运行结束")

if __name__ == '__main__':
    op=OrderPage()
    # 填写地址
    op.dz('wu1', '2411617685@qq.com', "河南省郑州市金水区", "123456")